<!DOCTYPE html>
<!-- 单图ocr -->
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OCR 文字识别</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            text-align: center;
        }
        .upload-area {
            border: 2px dashed #ccc;
            padding: 20px;
            margin: 20px 0;
            border-radius: 5px;
        }
        #preview {
            max-width: 100%;
            margin: 10px 0;
        }
        #result {
            margin-top: 20px;
            text-align: left;
        }
        .confidence {
            color: #666;
            font-size: 0.9em;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }
        
        .text-output {
            width: 100%;
            min-height: 200px;
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>OCR 文字识别系统</h1>
        <div class="upload-area">
            <input type="file" id="imageInput" accept="image/*">
            <p>支持的图片格式: JPG, PNG, BMP</p>
        </div>
        <button id="recognizeBtn" class="button" style="display: none;">开始识别</button>
        <img id="preview" style="display: none;">
        <div id="result"></div>
        <textarea id="outputText" class="text-output" readonly></textarea>
    </div>

    <script>
        document.getElementById('imageInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (!file) return;

            // 显示预览
            const preview = document.getElementById('preview');
            preview.style.display = 'block';
            preview.src = URL.createObjectURL(file);

            // 显示识别按钮
            document.getElementById('recognizeBtn').style.display = 'block';
        });

        document.getElementById('recognizeBtn').addEventListener('click', async function() {
            const file = document.getElementById('imageInput').files[0];
            if (!file) return;

            // 准备发送数据
            const formData = new FormData();
            formData.append('file', file);

            try {
                // 发送请求
                const response = await fetch('/ocr/', {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                // 显示结果
                const resultDiv = document.getElementById('result');
                resultDiv.innerHTML = '<h2>识别结果：</h2>';
                
                let fullText = '';
                data.text.forEach((text, index) => {
                    const confidence = (data.confidence[index] * 100).toFixed(2);
                    resultDiv.innerHTML += `
                        <p>
                            ${text}
                            <span class="confidence">(置信度: ${confidence}%)</span>
                        </p>
                    `;
                    fullText += text + '\n';
                });

                // 更新文本框内容
                document.getElementById('outputText').value = fullText;
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('result').innerHTML = '<p style="color: red;">识别失败，请重试</p>';
                document.getElementById('outputText').value = '';
            }
        });
    </script>
</body>
</html>